<!--  Ofri Keidar 302933981 group 05 -->

<%@page import="controller.EmployeeController"%>
<%@page import="model.database.DBException"%>
<%@page import="view.WebCommons"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id="productData" class="view.useBeans.ProductData"
	scope="page" />
<jsp:setProperty name="productData" property="*" />

<html>
<head>
<link rel="stylesheet" href="../../style/myTemplate/style.css"
	type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update product price</title>
</head>
<body>

	<span class="customHeader2">Update product price</span>

	<div id="page">
		<div class="post">

			<div id="sidebar">
				<ul>
					<li>
						<h2>Please enter new product's details</h2>
					</li>
				</ul>
			</div>
			<div id="content">
				<form METHOD=POST ACTION="UpdateProduct.jsp">
					<table>
						<tr>
							<td><span class="customFormValue">Name of product to
									update</span></td>
							<td><INPUT TYPE=TEXT NAME=name
								SIZE=<%=WebCommons.NAME_SIZE%>></td>
						</tr>
						<tr>
							<td><span class="customFormValue">New price</span></td>
							<td><INPUT TYPE=TEXT NAME=price
								SIZE=<%=WebCommons.NAME_SIZE%>></td>
						</tr>
					</table>

					<br> <INPUT TYPE=SUBMIT VALUE="update price"
						class="mySubmitButton">
				</form>
			</div>
		</div>
	</div>

	<%
		// get main menu page
		String menuPage = (String) session
				.getAttribute(WebCommons.MAIN_MANU);
	%>
	<div id="footer">
		<span class="myLink"><A HREF="<%="../../menus/" + menuPage%>">Back
				to Menu</A></span>
	</div>

	<%
		// check if already submitted details
		if (productData.isDataSet()) {

			// extract given details
			String name = productData.getName();
			double newPrice = productData.getPriceValue();

			// verify price's format
			if (Double.isNaN(newPrice)) {
				%>
				<span class="notify"><br>Please enter a positive number for
					the new price</span>
				<%
		} else {

				try {

					// extract controller from session			
					EmployeeController controller = (EmployeeController) session
							.getAttribute(WebCommons.CONTROLLER);

					// try updating product price
					boolean couldUpdate = controller.updateProductPrice(
							name, newPrice);

					// check success
					if (!couldUpdate) {

						// display error message
						%>
						<span class="notify"><br>Sorry, could not make update-
							product does not exist...</span>
						<%
					} else {

						// success
						%>
						<span class="notify"><br>Price updated successfully!</span>
						<%
					}

				} catch (DBException e) {

					e.printStackTrace(); // TODO DEBUG PRINT!!!

					// move to error page
					response.sendRedirect("../../commonPages/ErrorPage.jsp");
				}
			}
		}
	%>
</body>
</html>